package com.snail.xgcd.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.snail.xgcd.bean.*;
import com.snail.xgcd.mapper.UserMapper;
import com.snail.xgcd.service.TokenManager;
import com.snail.xgcd.service.UserService;
import com.snail.xgcd.utils.id.IdWorker;
import com.snail.xgcd.utils.id.IdWorkerBuider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenManager tokenManager;

    @Override
    public Single login(User user) {

        Single single = new Single();
        Map<String,Object> map = new HashMap<String,Object>();
//        User user = new User();
//        user.setPassword(password);
//        user.setUserName(userName);
        User mapperUser = userMapper.findUser(user);
        if (mapperUser==null){
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("账号密码错误,登录失败!");
        }else if(mapperUser.getUserName().equals(user.getUserName()) && mapperUser.getPassword().equals(user.getPassword())){
            //生成一个token，保存用户登录状态
            TokenModel tokenModel = tokenManager.createToken(mapperUser.getId());
            map.put("token",tokenModel.getUserId()+"_"+tokenModel.getToken());
            map.put("user",mapperUser);
            single.setData(map);
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("登录成功!");
        }else{
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("系统错误请联系管理员!");
        }
        return single;
    }

    @Override
    public Single logout(User user) {
        Single single = new Single();
        try{
            tokenManager.deleteToken(user.getId());
            single.setData(user);
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage(user.getName()+"退出登录成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("系统错误请联系管理员!");
        }
        return single;
    }

    @Override
    public Single findAll(User member,Integer pageSize,Integer pageNum) {
        Single single = new Single();
        try{
            if (pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum, pageSize);
            }
            List<User> userList = userMapper.findAll(member);
            PageInfo page = new PageInfo(userList);
            single.setData(userList);
            single.setCount(page.getTotal()+"");
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("获取用户列表成功！");
        }catch(Exception e){
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("获取用户列表失败！");
            e.printStackTrace();
        }
        return single;
    }

    @Override
    public Single deleteByPrimaryKey(String id) {
        Single single = new Single();
        try{
            userMapper.deleteByPrimaryKey(id);
            single.setData(id);
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("删除用户成功！");
        }catch(Exception e){
            e.printStackTrace();
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("删除用户失败！");
        }
        return single;
    }

    @Override
    public Single insert(User record) {
        Single single = new Single();
        try{
            record.setId(IdWorkerBuider.IDWORKER.nextId());
            record.setCreateDate(new Date());
            userMapper.insert(record);
            single.setData(record.getId());
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("添加用户成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setData(record.getId());
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("添加用户失败!");
        }
        return single;
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Single updateByPrimaryKeySelective(User record) {
        Single single = new Single();
        try{
//            record.setId(IdWorkerBuider.IDWORKER.nextId());
            userMapper.updateByPrimaryKeySelective(record);
            single.setData(record.getId());
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("修改用户成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setData(record.getId());
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("修改用户失败!");
        }
        return single;
    }

    @Override
    public Single updateByPrimaryKey(User record) {
        Single single = new Single();
        try{
//            record.setId(IdWorkerBuider.IDWORKER.nextId());
            userMapper.updateByPrimaryKey(record);
            single.setData(record.getId());
            single.setCode(Constant.SUCCESS[0]);
            single.setMessage("修改用户"+record.getId()+"成功!");
        }catch(Exception e){
            e.printStackTrace();
            single.setData(record.getId());
            single.setCode(Constant.FAILURE[0]);
            single.setMessage("修改用户"+record.getId()+"失败!");
        }
        return single;
    }
}
